package com.gengao.springbootcase.controller;

import com.gengao.springbootcase.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.io.SAXReader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.io.PipedOutputStream;
import java.util.UUID;
@Slf4j
@RestController
public class UploadControllerSelf {
    @RequestMapping(value = "uploadSelf",method = RequestMethod.POST)
    public Result uploadLocal(@RequestParam("image")  MultipartFile file) throws IOException {
        if(file==null){
            return Result.error("文件为空");
        }
        String originalFilename = file.getOriginalFilename();
        log.info("originalFilename：{}",originalFilename);
        String newFileName= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("newFileName:{}",newFileName);
        String localBase="E:\\";
        String newPath=localBase+newFileName;
        file.transferTo(new File(newPath));//保存到本地
        log.info("newPath:{}",newPath);
        return Result.success(newPath);
    }
}
